﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace _01_登录界面.Common
{
   public  class PasswordHelper
    {
        //附加属性
        public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password",
            typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnProperChanged)));

        public static string GetPassword(DependencyObject d)
        {
            return d.GetValue(PasswordProperty).ToString();
        }

        public static void SetPassword(DependencyObject d,string value)
        {
            d.SetValue(PasswordProperty,value);
        }
		private static void OnProperChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordbox = d as PasswordBox;
			if(!_isUpdating)
			passwordbox.Password = e.NewValue.ToString();
		}

        //-------------------------------------------------------------------
		public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Password",
		   typeof(string), typeof(PasswordHelper), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnAttach)));
		public static string GetAttack(DependencyObject d)
		{
			return d.GetValue(AttachProperty).ToString();
		}
		static bool _isUpdating = false;
		public static void SetAttack(DependencyObject d, string value)
		{
			d.SetValue(AttachProperty, value);
		}
		private static void OnAttach(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PasswordBox passwordbox = d as PasswordBox;
			passwordbox.Password += Password_PasswordChanged;
		}

		private static void Password_PasswordChanged(object sender, RoutedEventArgs e)
		{
			PasswordBox passwordBox = sender as PasswordBox;
			_isUpdating= true;
			SetPassword(passwordBox, passwordBox.Password);
			_isUpdating= false;
		}

	}
}
